package com.fengli.dao.impl;

import com.fengli.dao.AccountDao;
import com.fengli.entity.Account;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.util.List;

/******************************************************************************
 * @Package: [com.fengli.dao.impl.AccountDaoImpl]
 * @ClassName: [AccountDaoImpl]
 * @Description: [一句话描述该类的功能]
 * @Author: [fengli@founder.com.cn]  
 * @CreateDate: [2018/12/25 15:38]   
 * @UpdateUser: [fengli@founder.com.cn (如多次修改保留历史记录，增加修改记录)]   
 * @UpdateDate: [2018/12/25 15:38，(如多次修改保留历史记录，增加修改记录)]   
 * @UpdateRemark: [说明本次修改内容, (如多次修改保留历史记录 ， 增加修改记录)]
 * @Version: [v1.0]
 * ${tags}
 */
public class AccountDaoImpl implements AccountDao {
    private JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    /**
     * 该方法使用的是update(sql,Object ..argc),其中argc是参数
     *
     * @param account
     * @return
     */
    @Override
    public int addAccount(Account account) {
        Object objects[] = {
                account.getUsername(), account.getBalance()
        };
        int num = jdbcTemplate.update("insert into account(username,balance) values(?,?)", objects);
        return num;
    }

    @Override
    public int updateAccount(Account account) {
        return 0;
    }

    @Override
    public int deleteAccount(int id) {
        return 0;
    }

    @Override
    public Account findAccountByld(int id) {
        //定义 SQL 语句
        String sql = "select * from account where id = ? ";
    //创建 个新的 BeanPropertyRowMapper 对象
        RowMapper<Account> rowMapper = new BeanPropertyRowMapper<Account>(Account.class);
        //将 id 绑定到 SQL 语句中，井通过 RowMapper 返回 Object 类型的单行记录
        return this.jdbcTemplate.queryForObject(sql,rowMapper, id);
    }

    @Override
    public List<Account> findAIIAccount() {
        //定义 SQL 语句
        String sql = " select * from account ";
//创述 个新的 BeanPropertyRowMapper
        RowMapper<Account> rowMapper =new BeanPropertyRowMapper<Account>(Account.class);
//执行静态的 SQL 查询，并通过 RowMapper 返问结果
        return this.jdbcTemplate.query(sql,rowMapper) ;
    }


}
